//
//  XMAlbumController.m
//  XiaoMiao
//
//  Created by 23 on 2018/1/28.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMAlbumController.h"
#import "XMAlbumSubController.h"
#import "XMCameraController.h"
#import "XMCameraNavigationController.h"
#import "XMAlbumJigsawAvatarController.h"

@interface XMAlbumController () <XMMenuViewDelegate>
/** <#Description#> */
@property (nonatomic, strong) UIBarButtonItem *jigsawNavItem;
/** <#Description#> */
@property (nonatomic, strong) UIBarButtonItem *uploadNavItem;
@end

@implementation XMAlbumController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"图库";
    

    [self updateRightItemsIsCompare:0];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (self.dismissCallBlock) {
        self.dismissCallBlock();
    }
}

- (void)setupChildVc
{
    //设置菜单
    self.menuView.titles = @[@"照片图库", @"对比照图库"];
    self.menuView.delegate = self;
    
    //设置子控制器
    XMAlbumSubController *vc1 = [[XMAlbumSubController alloc]init];
    vc1.albumType = XMAlbumSubTypePhotos;
    vc1.partyId = self.partyId;
    vc1.realName = self.realName;
    vc1.isUpdataOperate = self.isUpdataOperate;
    
    XMAlbumSubController *vc2 = [[XMAlbumSubController alloc] init];
    vc2.albumType = XMAlbumSubTypeCompare;
    vc2.partyId = self.partyId;
    vc2.realName = self.realName;
    vc2.isUpdataOperate = self.isUpdataOperate;
    
    self.subControllers = [NSMutableArray arrayWithArray:@[vc1, vc2]];
}

// isCompare: 是否是“对比库”模块
- (void)updateRightItemsIsCompare:(BOOL)isCompare {
    if (self.isUpdataOperate) {
        self.navigationItem.rightBarButtonItems = @[];
    }else {
        self.navigationItem.rightBarButtonItems = @[];
        
        if (isCompare) {
            self.navigationItem.rightBarButtonItems = @[self.uploadNavItem];
        }else {
            self.navigationItem.rightBarButtonItems = @[self.jigsawNavItem, self.uploadNavItem];
        }
    }
}

//上传更新照片状态
- (void)actionUpload
{
    XMAlbumController *vc = [[XMAlbumController alloc] init];
    vc.partyId = self.partyId;
    vc.isUpdataOperate = YES;
    vc.defaultIndex = self.menuView.selectedIndex;
    vc.realName = self.realName;
    [self.navigationController pushViewController:vc animated:YES];
}

//拼图
- (void)actionJigsaw
{
    XMAlbumJigsawAvatarController *jigsaw = [[XMAlbumJigsawAvatarController alloc] init];
    jigsaw.partyId = self.partyId;
    [self.navigationController pushViewController:jigsaw animated:YES];
}

#pragma mark - XMMenuViewDelegate
- (void)menuView:(XMMenuView *)menuView didMenuBtnClicked:(UIButton *)button
{
    [super menuView:menuView didMenuBtnClicked:button];
    if (menuView.selectedIndex == 0) { // 照片图库
        [self updateRightItemsIsCompare:NO];
    }else if (menuView.selectedIndex == 1) { // 对比照图库
        [self updateRightItemsIsCompare:YES];
    }
}

#pragma mark - Lazy
- (UIBarButtonItem *)jigsawNavItem {
    if (!_jigsawNavItem) {
        UIButton *jigsawButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [jigsawButton setTitle:@"拼图" forState:UIControlStateNormal];
        [jigsawButton setTitleColor:kRGBColor(37, 179, 252) forState:UIControlStateNormal];
        jigsawButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        jigsawButton.frame = CGRectMake(0, 0, 40, 20); // llq-note 暂时这样处理
        [jigsawButton addTarget:self action:@selector(actionJigsaw) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *jigsawNav = [[UIBarButtonItem alloc] initWithCustomView:jigsawButton];
        
        _jigsawNavItem = jigsawNav;
    }
    return _jigsawNavItem;
}

- (UIBarButtonItem *)uploadNavItem {
    if (!_uploadNavItem) {
        UIButton *uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [uploadButton setTitle:@"上传" forState:UIControlStateNormal];
        [uploadButton setTitleColor:kRGBColor(37, 179, 252) forState:UIControlStateNormal];
        uploadButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        uploadButton.frame = CGRectMake(0, 0, 40, 20);
        [uploadButton addTarget:self action:@selector(actionUpload) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *uploadNav = [[UIBarButtonItem alloc] initWithCustomView:uploadButton];
        
        _uploadNavItem = uploadNav;
    }
    return _uploadNavItem;
}
@end
